#delim cr
set more off
*version 11
pause on
graph set ps logo off

capture log close
set linesize 250
set logtype text
log using ../log/pull-blry-controls.log , replace

/* --------------------------------------

This program pulls key controls
from the BLRY (weekly) data to be merged
into our daily data.

--------------------------------------- */

clear all
estimates clear

************************************************************
**   Describe the BLRY data in our possession
************************************************************

describe using ../src/bloom-data/attrition.dta
describe using ../src/bloom-data/conversion.dta
describe using ../src/bloom-data/daysathome.dta
describe using ../src/bloom-data/exhaustion.dta
describe using ../src/bloom-data/negative.dta
describe using ../src/bloom-data/performance_during_exper.dta
describe using ../src/bloom-data/perform_top_bottom.dta
describe using ../src/bloom-data/positive.dta
describe using ../src/bloom-data/post_sep_home.dta
describe using ../src/bloom-data/promotion.dta
describe using ../src/bloom-data/quit_data.dta
describe using ../src/bloom-data/recording.dta
describe using ../src/bloom-data/satisfaction.dta
describe using ../src/bloom-data/selection.dta
describe using ../src/bloom-data/selection_new.dta
describe using ../src/bloom-data/summary_volunteer.dta
describe using ../src/bloom-data/switch_gohome.dta
describe using ../src/bloom-data/switch_return.dta
describe using ../src/bloom-data/tc_comparison.dta
describe using ../src/bloom-data/treatment_effect.dta
describe using ../src/bloom-data/wage_new.dta
describe using ../src/bloom-data/wage_wfh.dta


************************************************************
**   Pull time-invariant summary characteristics
************************************************************

preserve
	use ../src/bloom-data/summary_volunteer.dta , clear

	isid personid

	keep personid age tenure grosswage children bedroom commute men married volunteer high_educ 

	tempfile time_invariant
	save `time_invariant'
restore

************************************************************
**   Bring in the experimental overall performance Bloom data
************************************************************

preserve

	use ../src/bloom-data/performance_during_exper.dta , clear

	isid personid year_week
	codebook personid

	** Pick out a few performance observations
	foreach period in 201005 201030 201105 201130 {
		gen temp_`period' = (year_week == `period') * phonecallraw
		egen blry_numcall_`period' = max(temp_`period') , by(personid)
		replace blry_numcall_`period' = . if blry_numcall_`period' == 0
	}

	** Now pick out that experimental group
	tab expgroup , miss
	egen min_expgroup = min(expgroup) , by(personid)
	egen max_expgroup = max(expgroup) , by(personid)
	count
	count if min_expgroup != max_expgroup

	** Now only have one observation per worker
	bysort personid: keep if _n == 1
	keep personid blry_numcall_* expgroup

	tempfile performance
	save `performance'
restore

************************************************************
**   Bring in the quitting data
************************************************************

preserve

	use ../src/bloom-data/quit_data.dta , clear

	isid personid

	** We only pick out the time-invariant controls
	keep personid quitjob costofcommute children
	isid personid

	tempfile quits
	save `quits'
restore

************************************************************
**   Bring in return-from-home data
************************************************************

preserve

	use ../src/bloom-data/switch_return.dta , clear

	isid personid 

	d, f
	keep personid return

	tempfile returns
	save `returns'

restore

************************************************************
**   Create table indicators
************************************************************

** BLRY Table I studies who volunteers. BLRY Table II measures the effect of the
** experiment. We create indicators for workers being in those two samples.

** Table 1:
preserve
	use ../src/bloom-data/summary_volunteer.dta, clear
	dprobit volunteer children married commute bedroom high_educ tenure grosswage age men
	gen byte table_I_sample = e(sample)
	tab table_I_sample
	
	keep personid table_I_sample

	tempfile table1
	save `table1'
restore

** Table 2:
preserve
	use ../src/bloom-data/performance_during_exper.dta, clear
	xi:areg logcalllength experiment_treatment i.year_week if year_week~=201049 & (expgroup==1|expgroup==0) & logphonecall~=. & phonecallraw>20, ab(personid) cluster(personid)
	gen byte temp_table_II_sample = e(sample)
	tab temp_table_II_sample

	egen table_II_sample = max(temp_table_II_sample) , by(personid)
	
	bysort personid: keep if _n == 1
	keep personid table_II_sample

	tempfile table2
	save `table2'
restore

************************************************************
**   Stack what we have
************************************************************

use `performance' , clear
merge 1:1 personid using `quits'
drop _merge

merge 1:1 personid using `returns'
drop _merge

merge 1:1 personid using `time_invariant'
drop _merge

merge 1:1 personid using `table1'
drop _merge

merge 1:1 personid using `table2'
drop _merge

************************************************************
**   Create EmployeeID
************************************************************

codebook personid
gen EmployeeID_stub = personid / 2 - 1000
gen str10 EmployeeID = "S" + string(EmployeeID_stub) if EmployeeID_stub > 10000
replace EmployeeID = "S" + "0" + string(EmployeeID_stub) if EmployeeID_stub < 10000 & EmployeeID_stub > 1000
replace EmployeeID = "S" + "00" + string(EmployeeID_stub) if EmployeeID_stub < 1000 & EmployeeID_stub > 100
replace EmployeeID = "S" + "000" + string(EmployeeID_stub) if EmployeeID_stub < 100 & EmployeeID_stub > 10
replace EmployeeID = "S" + "0000" + string(EmployeeID_stub) if EmployeeID_stub < 10
codebook EmployeeID
drop EmployeeID_stub

************************************************************
**   Save & Close
************************************************************

compress
save ../dta/blry-time-invariant-controls.dta , replace

d, f
sum
isid EmployeeID



log close
exit

